//接码平台API-V3.1
//使用前请仔细查看以下参数
//From.TheHot.2018.10.28

/*
var 接码平台 = "易码"; //关键词（共享、短租、火云、易码），其他平台有待添加。
var 平台账号 = ""; //登陆用户名或API(火云和共享一定要用API账号)
var 登陆密码 = ""; //登陆密码
var 项目ID = ""; //接码项目ID
var 虚拟号设置 = "0"; //0为正常号段，1为虚拟号段，为空则随机
*/

//变量转换
var CodePlatform = 接码平台;
var Account_Api = 平台账号;
var PassWord = 登陆密码;
var Project_ID = 项目ID;
var Provinces = ""; //是否按省份获取，为空则随机，省份按照平台显示名称填写
var PhoneType = ""; //CMCC 为移动，UNICOM 为联通，TELECOM 为电信,为空则随机
var vno = 虚拟号设置;
//易码特殊设置项
if (vno == 0) {
    var excludeno = "170.171.180";
} else {
    var excludeno = "";
}
var ProvincesCode = ""; //易码平台省份设置（填写省份代码，易码平台获取）
var isp = ""; //号码所属运营商代码。1:移动，2:联通，3:电信，为空则随机

//接码平台API地址
var gx_api = "http://api.ipadh.cn/do.php?action=";
var hy_api = "http://embshop.net/api/do.php?action=";
var dz_api = "http://api.jmyzm.com/http.do?action=";
var ym_api = "http://api.fxhyd.cn/UserInterface.aspx?action=";
var dm_api = "http://api.duomi01.com/api?action=";

var LoginToken = getToken(CodePlatform);

//获取token值
function getToken(CodePlatform) {
    switch (CodePlatform) {
        case "共享":
            var r = http.get(gx_api + "loginIn&name=" + Account_Api + "&password=" + PassWord);
            var token = r.body.string().split("|");
            if (token[0] == 0) {
                toastLog(token[1]);
                return 0;
            }
            return token[1];
            break;
        case "火云":
            var r = http.get(hy_api + "loginIn&name=" + Account_Api + "&password=" + PassWord);
            var token = r.body.string().split("|");
            if (token[0] == 0) {
                toastLog(token[1]);
                return 0;
            }
            return token[1];
            break;
        case "多米":
            var r = http.get(dm_api + "loginIn&name=" + Account_Api + "&password=" + PassWord);
            var token = r.body.string().split("|");
            if (token[0] == 0) {
                toastLog(token[1]);
                return 0;
            }
            return token[1];
            break;
        case "短租":
            var r = http.get(dz_api + "loginIn&uid=" + Account_Api + "&pwd=" + PassWord);
            var token = r.body.string();
            if (token.indexOf(Account_Api) == -1) {
                toastLog("短租账号登陆失败！");
                return 0;
            }
            token = token.split("|");
            return token[1];
            break;
        case "易码":
            var r = http.get(ym_api + "login&username=" + Account_Api + "&password=" + PassWord);
            var token = r.body.string();
            if (token.indexOf("success") == -1) {
                toastLog("易码账号登陆失败！");
                return 0;
            }
            token = token.split("|");
            return token[1];
            break;
        case "神话":
            break;
        case "蚂蚁":
            break;
        default:
            toast("未接入该接码平台！");
    }

}

//获取手机号函数
function GetPhone(phone) {
    switch (CodePlatform) {
        case "共享":
            if (phone != null) {
                var r = http.get(gx_api + "getPhone&sid=" + Project_ID + "&token=" + LoginToken + "&phone=" + phone);
                phone = r.body.string().split("|");
                if (phone[0] == 0) {
                    toastLog(phone[1]);
                    return 0;
                    break;
                }
                return phone[1];
                break;
            }
            var r = http.get(gx_api + "getPhone&sid=" + Project_ID + "&token=" + LoginToken + "&locationMatching=include&locationLevel=p&location=" + Provinces + "&showExtra=1&phoneType=" + PhoneType + "&vno=" + vno);
            phone = r.body.string().split("|");
            if (phone[0] == 0) {
                toastLog(phone[1]);
                return 0;
                break;
            }
            return phone[1];
            break;
        case "火云":
            if (phone != null) {
                var r = http.get(hy_api + "getPhone&sid=" + Project_ID + "&token=" + LoginToken + "&phone=" + phone);
                phone = r.body.string().split("|");
                if (phone[0] == 0) {
                    toastLog(phone[1]);
                    return 0;
                    break;
                }
                return phone[1];
                break;
            }
            var r = http.get(hy_api + "getPhone&sid=" + Project_ID + "&token=" + LoginToken + "&locationMatching=include&locationLevel=p&location=" + Provinces + "&showExtra=1&phoneType=" + PhoneType + "&vno=" + vno);
            phone = r.body.string().split("|");
            if (phone[0] == 0) {
                toastLog(phone[1]);
                return 0;
                break;
            }
            return phone[1];
            break;
        case "多米":
            if (phone != null) {
                var r = http.get(dm_api + "getPhone&sid=" + Project_ID + "&token=" + LoginToken + "&phone=" + phone);
                phone = r.body.string().split("|");
                if (phone[0] == 0) {
                    toastLog(phone[1]);
                    return 0;
                    break;
                }
                return phone[1];
                break;
            }
            var r = http.get(dm_api + "getPhone&sid=" + Project_ID + "&token=" + LoginToken + "&locationMatching=include&locationLevel=p&location=" + Provinces + "&showExtra=1&phoneType=" + PhoneType + "&vno=" + vno);
            phone = r.body.string().split("|");
            if (phone[0] == 0) {
                toastLog(phone[1]);
                return 0;
                break;
            }
            return phone[1];
            break;
        case "短租":
            if (phone != null) {
                var r = http.get(dz_api + "getMobilenum&pid=" + Project_ID + "&uid=" + Account_Api + "&token=" + LoginToken + "&mobile=" + phone);
                phone = r.body.string();
                if (phone.indexOf(LoginToken) == -1) {
                    toastLog("获取号码失败，请检查账户信息及项目ID！");
                    return 0;
                    break;
                }
                phone = phone.split("|");
                return phone[0];
                break;
            }
            var r = http.get(dz_api + "getMobilenum&pid=" + Project_ID + "&uid=" + Account_Api + "&token=" + LoginToken + "&mobile=&size=1&province=" + Provinces + "&showExtra=1&operator=" + PhoneType + "&vno=" + vno);
            phone = r.body.string();
            if (phone.indexOf(LoginToken) == -1) {
                toastLog("获取号码失败，请检查账户信息及项目ID！");
                return 0;
                break;
            }
            phone = phone.split("|");
            return phone[0];
            break;
        case "易码":
            if (phone != null) {
                var r = http.get(ym_api + "getmobile&token=" + LoginToken + "&itemid=" + Project_ID + "&mobile" + phone);
                phone = r.body.string();
                if (phone.indexOf("success") == -1) {
                    toastLog("获取号码失败，请检查账户信息及项目ID！");
                    return 0;
                    break;
                }
                phone = phone.split("|");
                return phone[1];
                break;
            }
            var r = http.get(ym_api + "getmobile&token=" + LoginToken + "&itemid=" + Project_ID + "&excludeno=" + excludeno + "&province=" + ProvincesCode + "&isp=" + isp);
            phone = r.body.string();
            if (phone.indexOf("success") == -1) {
                toastLog("获取号码失败，请检查账户信息及项目ID！");
                return 0;
                break;
            }
            phone = phone.split("|");
            return phone[1];
            break;
        case "神话":
            break;
        case "蚂蚁":
            break;
        default:
            toast("未接入该接码平台！");
    }
}

//获取验证码函数
function GetCode(phone, setOverTime) {
    switch (CodePlatform) {
        case "共享":
            var tag = 0;
            var k = 0;
            while (tag == 0 && k < Math.ceil(setOverTime / 3)) {
                toastLog("未获取到短信,等待3秒重试！");
                sleep(3000);
                var r = http.get(gx_api + "getMessage&phone=" + phone + "&sid=" + Project_ID + "&token=" + LoginToken);
                var code = r.body.string().split("|");
                tag = code[0];
                code = code[1];
                k = k + 1;
            }
            if (tag == 0) {
                toastLog("获取验证码超时或失败！");
                return 0;
            }
            code = code.match(/\d+/g);
            return code[0];
            break;
        case "火云":
            var tag = 0;
            var k = 0;
            while (tag == 0 && k < Math.ceil(setOverTime / 3)) {
                toastLog("未获取到短信,等待3秒重试！");
                sleep(3000);
                var r = http.get(hy_api + "getMessage&phone=" + phone + "&sid=" + Project_ID + "&token=" + LoginToken);
                var code = r.body.string().split("|");
                tag = code[0];
                code = code[1];
                k = k + 1;
            }
            if (tag == 0) {
                toastLog("获取验证码超时或失败！");
                return 0;
            }
            code = code.match(/\d+/g);
            return code[0];
            break;
        case "多米":
            var tag = 0;
            var k = 0;
            while (tag == 0 && k < Math.ceil(setOverTime / 3)) {
                toastLog("未获取到短信,等待3秒重试！");
                sleep(3000);
                var r = http.get(dm_api + "getMessage&phone=" + phone + "&sid=" + Project_ID + "&token=" + LoginToken);
                var code = r.body.string().split("|");
                tag = code[0];
                code = code[1];
                k = k + 1;
            }
            if (tag == 0) {
                toastLog("获取验证码超时或失败！");
                return 0;
            }
            code = code.match(/\d+/g);
            return code[0];
            break;
        case "短租":
            var tag = 0;
            var k = 0;
            while (tag != phone && k < Math.ceil(setOverTime / 3)) {
                toastLog("未获取到短信,等待3秒重试！");
                sleep(3000);
                var r = http.get(dz_api + "getVcodeAndReleaseMobile&uid=" + Account_Api + "&token=" + LoginToken + "&mobile=" + phone);
                var code = r.body.string().split("|");
                tag = code[0];
                code = code[1];
                k = k + 1;
            }
            if (tag != phone) {
                toastLog("获取验证码超时或失败！");
                return 0;
            }
            code = code.match(/\d+/g);
            return code[0];
            break;
        case "易码":
            var tag = 0;
            var i = 0;
            while (tag != "success" && i < Math.ceil(setOverTime / 3)) {     
                var r = http.get(ym_api + "getsms&token=" + LoginToken + "&itemid=" + Project_ID + "&mobile=" + phone);
                var code = r.body.string().split("|");
                tag = code[0];
                code = code[1];
                i = i + 1;
                toastLog("未获取到短信,等待3秒重试！");
                sleep(3000);
            }
            if (tag != "success") {
                toastLog("获取验证码超时或失败！");
                return 0;
            }
            code = code.match(/\d+/g);
            return code[0];
            break;
        case "神话":
            break;
        case "蚂蚁":
            break;
        default:
            toast("未接入该接码平台！");
    }
}

//释放手机号函数
function rec(phone) {
    switch (接码平台) {
        case "共享":
            var r = http.get(gx_api + "cancelRecv&phone=" + phone + "&sid=" + Project_ID + "&token=" + LoginToken);
            var sfphone = r.body.string().split("|");
            if (sfphone[0] == 1) {
                return 1;
            } else {
                return 0;
            }
            break;
        case "火云":
            var r = http.get(hy_api + "cancelRecv&sid=" + Project_ID + "&phone=" + phone + "&token=" + LoginToken);
            var sfphone = r.body.string().split("|");
            if (sfphone[0] == 1) {
                return 1;
            } else {
                return 0;
            }
            break;
        case "多米":
            var r = http.get(dm_api + "cancelRecv&sid=" + Project_ID + "&phone=" + phone + "&token=" + LoginToken);
            var sfphone = r.body.string().split("|");
            if (sfphone[0] == 1) {
                return 1;
            } else {
                return 0;
            }
            break;
        case "短租":
            var r = http.get(dz_api + "addIgnoreList&uid=" + Account_Api + "&token=" + LoginToken + "&mobiles=" + phone + "&pid=" + Project_ID);
            var sfphone = r.body.string();
            if (sfphone == 1) {
                return 1;
            } else {
                return 0;
            }
            break;
        case "易码":
            var r = http.get(ym_api + "release&token=" + LoginToken + "&itemid=" + Project_ID + "&mobile=" + phone);
            var sfphone = r.body.string();
            if (sfphone == "success") {
                return 1;
            } else {
                return 0;
            }
            break;
        case "神话":
            break;
        case "蚂蚁":
            break;
        default:
            toast("未接入该接码平台！");
    }
}

//讯代理优质

/* 
**  var 讯代理spiderId = "e1646d4003b34271a70a5bebc4abe56a";
**  var 优质代理订单号 = "YZ20188168670Q4k4pr";
**  xdaili_youzhi(讯代理spiderId,优质代理订单号);
*/

function xdaili_youzhi(spiderId, orderNo) {
    var spId = spiderId; //spiderId
    var odNo = orderNo; //订单号
    var getIp_api = http.get('http://pv.sohu.com/cityjson?ie=utf-8');
    var InetIP = getIp_api.body.string();
    eval(InetIP);
    log("代理前外网IP:" + returnCitySN.cip);
    var xdailiUrl = "http://api.xdaili.cn/xdaili-api//greatRecharge/getGreatIp?spiderId=" + spId + "&orderno=" + odNo + "&returnType=2&count=1";
    for (let i = 0; i < 3; i++) {
        try {
            var getProxy_json = http.get(xdailiUrl).body.json();

            if (getProxy_json.ERRORCODE == "10036" || getProxy_json.ERRORCODE == "10038" || getProxy_json.ERRORCODE == "10055") {
                throw {
                    "code": "-1",
                    "msg": "提取速度过快"
                }
            }
            if (getProxy_json.ERRORCODE == "10036") {
                throw {
                    "code": "-2",
                    "msg": "提取次数已达上限"
                }
            }
            if (getProxy_json.ERRORCODE == "10063") {
                throw {
                    "code": "-3",
                    "msg": "请输入正确的spiderId！"
                }
            }
            if (getProxy_json.ERRORCODE == "10073") {
                throw {
                    "code": "-4",
                    "msg": "订单不存在！"
                }
            }
            break;
        } catch (e) {
            if (e.code == "-1") {
                log(e.msg);
                sleep(5000);
            }
            if (e.code == "-2") {
                log(e.msg);
                exit();
            }
            if (e.code == "-3") {
                log(e.msg);
                exit();
            }
            if (e.code == "-4") {
                log(e.msg);
                exit();
            }
            if (i == 2) { //运行3次后,没有拿到IP和端口 程序结束
                log("发生了未知错误,请检查网络...");
                exit();
            }
        }
    }
    var xdaili_proxyIP = getProxy_json.RESULT[0].ip;
    var xdaili_proxyPort = parseInt(getProxy_json.RESULT[0].port);
    httpProxy(xdaili_proxyIP, xdaili_proxyPort);
    var getIp_api = http.get('http://pv.sohu.com/cityjson?ie=utf-8');
    var InetIP = getIp_api.body.string();
    eval(InetIP);
    log("代理后外网IP:" + returnCitySN.cip);
}


function httpProxy(url, prot) {
    var Proxy = java.net.Proxy;
    var InetSocketAddress = java.net.InetSocketAddress;
    var okhttp = new Packages.okhttp3.OkHttpClient.Builder().proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(url, prot)));
    http.__okhttp__.muteClient(okhttp);
}


function lianzhong(e, t, a) {
    http.__okhttp__.setTimeout(3e4);
    var o = images.load(a),
        r = images.toBase64(o, format = "png"),
        i = device.release,
        c = device.model,
        s = device.buildId;
    try {
        var n = http.postJson("https://v2-api.jsdama.com/upload", {
            softwareId: 11899,
            softwareSecret: "Jyyh42S6ntMYzVK4msI1xQDpq2YJM7gMo0OtfoYp",
            username: e,
            password: t,
            captchaData: r,
            captchaType: 1001,
            captchaMinLength: 0,
            captchaMaxLength: 0,
            workerTipsId: 0
        }, {
            headers: {
                Host: "v2-api.jsdama.com",
                Connection: "keep-alive",
                "Content-Length": "298",
                Accept: "application/json, text/javascript, */*; q=0.01",
                "User-Agent": "Mozilla/5.0 (Linux; Android " + i + "; " + c + " Build/" + s + "; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 Mobile Safari/537.36",
                "Content-Type": "text/json"
            }
        });
    } catch (e) {
        return {
            code: "-1",
            msg: "网络链接超时...",
            data: {}
        };
    }
    var d = n.body.json(),
        p = d.code,
        m = d.message;
    if ("10079009" == p) return {
        code: p,
        msg: m,
        data: {}
    };
    if ("10142006" == p) return {
        code: p,
        msg: m,
        data: {}
    };
    if ("10142004" == p) return {
        code: p,
        msg: m,
        data: {}
    };
    if ("10142005" == p) return {
        code: p,
        msg: m,
        data: {}
    };
    if ("10079006" == p) return {
        code: p,
        msg: m,
        data: {}
    };
    if ("0" == p) {
        return {
            code: p,
            msg: m,
            data: {
                res: d.data.recognition
            }
        };
    }
    return d;
}
